package cn.iocoder.yudao.module.member.service.usercidbind;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.controller.admin.usercidbind.vo.UserCidBindBaseVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.app.usercidbind.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.usercidbind.UserCidBindDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.member.convert.usercidbind.UserCidBindConvert;
import cn.iocoder.yudao.module.member.dal.mysql.usercidbind.UserCidBindMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 推送绑定的id Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserCidBindServiceImpl implements UserCidBindService {

    @Resource
    private UserCidBindMapper userCidBindMapper;

    @Override
    public Long createUserCidBind(AppUserCidBindCreateReqVO createReqVO) {
        // 插入
        UserCidBindDO userCidBind = UserCidBindConvert.INSTANCE.convert(createReqVO);
        userCidBindMapper.insert(userCidBind);
        // 返回
        return userCidBind.getId();
    }

    @Override
    public void updateUserCidBind(AppUserCidBindUpdateReqVO updateReqVO) {
        // 校验存在
        validateUserCidBindExists(updateReqVO.getId());
        // 更新
        UserCidBindDO updateObj = UserCidBindConvert.INSTANCE.convert(updateReqVO);
        userCidBindMapper.updateById(updateObj);
    }
    @Override
    public void saveOrUpdateCidBind(AppUserCidBindCreateReqVO createReqVO) {
        UserCidBindDO userCidBindDO = userCidBindMapper.selectOne(UserCidBindDO::getUserId, SecurityFrameworkUtils.getLoginUserId());

        if (userCidBindDO == null) {

            // 插入
            createReqVO.setUserId(SecurityFrameworkUtils.getLoginUserId());
            UserCidBindDO userCidBind = UserCidBindConvert.INSTANCE.convert(createReqVO);
            userCidBindMapper.insert(userCidBind);
        }else{
            // 更新
            userCidBindDO.setCid(createReqVO.getCid());
            userCidBindMapper.updateByUserId(userCidBindDO.getUserId(),createReqVO.getCid());
        }

    }
    @Override
    public void deleteUserCidBind(Long id) {
        // 校验存在
        validateUserCidBindExists(id);
        // 删除
        userCidBindMapper.deleteById(id);
    }

    private void validateUserCidBindExists(Long id) {
        if (userCidBindMapper.selectById(id) == null) {
            throw exception(USER_CID_BIND_NOT_EXISTS);
        }
    }

    @Override
    public UserCidBindDO getUserCidBind(Long id) {
        return userCidBindMapper.selectById(id);
    }

    @Override
    public List<UserCidBindDO> getUserCidBindByUserIds(List<Long> userIds) {
        return userCidBindMapper.selectList(new LambdaQueryWrapper<UserCidBindDO>().in(UserCidBindDO::getUserId,userIds));
    }

    @Override
    public List<UserCidBindDO> getUserCidBindList(Collection<Long> ids) {
        return userCidBindMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<UserCidBindDO> getUserCidBindPage(AppUserCidBindPageReqVO pageReqVO) {
        return userCidBindMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserCidBindDO> getUserCidBindList(AppUserCidBindExportReqVO exportReqVO) {
        return userCidBindMapper.selectList(exportReqVO);
    }

}
